package com.smartscreen.baseinfomanage.service.impl;

import com.smartscreen.baseinfomanage.dto.CreateVenueRequest;
import com.smartscreen.baseinfomanage.entity.Venue;
import com.smartscreen.baseinfomanage.mapper.VenueMapper;
import com.smartscreen.baseinfomanage.service.VenueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class VenueServiceImpl implements VenueService {

    @Autowired
    private VenueMapper venueMapper;

    @Override
    public List<Venue> getAllVenues() {
        return venueMapper.selectList(null);
    }

    @Override
    public String createVenue(CreateVenueRequest request) {
        String venueId = "v" + UUID.randomUUID().toString().substring(0, 6);
        
        Venue newVenue = new Venue();
        newVenue.setVenueId(venueId);
        newVenue.setName(request.getName());
        newVenue.setCapacity(request.getCapacity());
        newVenue.setType(request.getType());
        
        venueMapper.insert(newVenue);
        return venueId;
    }

    // ... existing code ...

    @Override
    public Map<String, Map<String, Object>> getVenuesBatchAsMap(List<String> venueIds) {
        if (venueIds == null || venueIds.isEmpty()) {
            return new HashMap<>();
        }

        // 去重并过滤空值
        List<String> validIds = venueIds.stream()
                .filter(Objects::nonNull)
                .filter(id -> !id.trim().isEmpty())
                .distinct()
                .collect(Collectors.toList());

        if (validIds.isEmpty()) {
            return new HashMap<>();
        }

        // 批量查询场地信息
        List<Venue> venues = venueMapper.selectBatchIds(validIds);

        // 转换为Map格式
        Map<String, Map<String, Object>> result = new HashMap<>();
        for (Venue venue : venues) {
            Map<String, Object> venueMap = new HashMap<>();
            venueMap.put("venueId", venue.getVenueId());
            venueMap.put("name", venue.getName());
            venueMap.put("capacity", venue.getCapacity());
            venueMap.put("type", venue.getType());
            venueMap.put("createdAt", venue.getCreatedAt());
            venueMap.put("updatedAt", venue.getUpdatedAt());

            result.put(venue.getVenueId(), venueMap);
        }

        return result;
    }

// ... existing code ...
}